import { DirUtil } from "./DirUtil";
import { Main } from "../Main";

/**
 * 检测ne工具
 * @author 赵占涛
 */
export class CheckNeUtil {
    /** 检测 */
    static check(cb:()=>void) {
        console.log("预检测ne");
        this.checkAnsRepeat();
        this.checkForgetDeleteAni();
        cb();
    }

    /** 检测ans重名 */
    private static checkAnsRepeat() {
        let allFile = DirUtil.getAllFile(Main.projPath + "public/art/cocosstudio/cocosstudio/neAni/ans/")
        // 文件重复map
        let fileHas:{[key:string]:true} = {};
        // 重复的文件
        let repeatedFile:string[] = [];
        for (let i = 0; i < allFile.length; i++) {
            const file = allFile[i];
            let realFile = file.substr(file.lastIndexOf("/") + 1).replace(".ans", "");
            if (fileHas[realFile]) {
                repeatedFile.push(realFile);
            } else {
                fileHas[realFile] = true;
            }
        }
        if (repeatedFile.length > 0) {
            console.log("有重名的ans文件", repeatedFile);
            throw new Error("有重名的ans文件，找付配吉解决");
        }
    }
    /** 检测忘删的ani文件（忘删的定义是，没有ans但有ani） */
    private static checkForgetDeleteAni() {
        let forgetDeleteAni:string[] = [];
        let allAnsFile = DirUtil.getAllFile(Main.projPath + "public/art/cocosstudio/cocosstudio/neAni/ans/")
        // ans纯文件名map
        let ansMap:{[key:string]:true} = {};
        for (let i = 0; i < allAnsFile.length; i++) {
            const file = allAnsFile[i];
            let realFile = file.substr(file.lastIndexOf("/") + 1).replace(".ans", "");
            ansMap[realFile] = true;
        }
        let allAniFile = DirUtil.getAllFile(Main.projPath + "public/art/cocosstudio/cocosstudio/neAni/ani/")
        for (let i = 0; i < allAniFile.length; i++) {
            const file = allAniFile[i];
            let realFile = file.substr(file.lastIndexOf("/") + 1).replace(".ani", "");
            if (!ansMap[realFile]) {
                forgetDeleteAni.push(realFile);
            }
        }
        if (forgetDeleteAni.length > 0) {
            console.log("有忘删除的ani文件", forgetDeleteAni);
            throw new Error("有忘删除的ani文件，找付配吉解决");
        }        
    }
}